/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package fanta.framework.dao;
import fanta.framework.manager.ConnectionManager;
import fanta.framework.persistence.DateGiornate;
import fanta.framework.persistence.Giocatore;
import fanta.framework.persistence.Voti;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
public class VotiDAO {
private static final Logger logger = Logger.getLogger(UtenteDAO.class);
private static Voti creaVoti (Giocatore idgiocatore, DateGiornate data , double voto, ConnectionManager cm ){
Voti ut = new Voti();
 EntityManager entityManager = (EntityManager) cm.getEntityManager();
        try {
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            ut.setData(data);
            ut.setIdgiocatore(idgiocatore);
            ut.setVoto(voto);
            entityManager.persist(ut);
            logger.debug("Transaction persisted");
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            logger.info("Voto giocatore:"+ut);

        } catch (Exception e) {
            logger.error("Errore in VotiDAO", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }
        return ut;

}
public static boolean aggiornaVoti(Voti voto, ConnectionManager cm) {
        boolean result = false;
            EntityManager entityManager = (EntityManager) cm.getEntityManager();
        try {
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            entityManager.persist(entityManager.merge(voto));
            logger.debug("Transaction persisted");
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            result = true;
        } catch (Exception e) {
            logger.error("Errore in aggiornaVotiDAO", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }
        return result;
    }

public static boolean cancellaVoti(Integer id, ConnectionManager cm) {
        boolean result = false;
        EntityManager entityManager = (EntityManager) cm.getEntityManager();
        try{
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            entityManager.remove(entityManager.find(Voti.class, id));
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            result = true;
        } catch (Exception e) {
            logger.error("Errore in cancellaVotiDAO", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }

        return result;
    }
public static Voti cercaVotoById(Integer id, ConnectionManager cm) {
        EntityManager entityManager = (EntityManager) cm.getEntityManager();
        Voti ut = new Voti();
        try {
            Object obj = entityManager.find(Voti.class, id);
            if(obj != null) {
                ut = (Voti)obj;
            }
        }catch (Exception e) {
            logger.error("Errore in CercaVotiDAO", e);
        }
        return ut;
   }
}
